﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChineseSchool.BLL;
using ChineseSchool.Models.Hethong;
using System.Data;

namespace ChineseSchool.Admin
{
    public partial class QL_Menu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BinGrid();
                btnAdd.Visible = true;
                btnEdit.Visible = true;
            }
        }

        protected void BinGrid()
        {
            B_Menu B_Menu = new B_Menu();
            DataTable lstM_Menu = B_Menu.GetAll();
            grdMenu.DataSource = lstM_Menu;
            grdMenu.DataBind();
        }

        protected void Bin_dllMenuParent()
        {
            List<M_Menu> lstM_Menu = (new B_Menu()).GetAlls();
            ddlMenuParent.DataSource = lstM_Menu;
            ddlMenuParent.DataValueField = "MenuID";
            ddlMenuParent.DataTextField = "MenuName";
            ddlMenuParent.DataBind();
            ddlMenuParent.Items.Insert(0, new ListItem("----Chọn danh mục cha----", string.Empty));
        }

        protected void Bin_ddlMenuType()
        {
            List<M_MenuType> lstM_MenuType = (new B_MenuType()).GetAlls();
            ddlMenuType.DataSource = lstM_MenuType;
            ddlMenuType.DataValueField = "MenuTypeID";
            ddlMenuType.DataTextField = "MenuTypeName";
            ddlMenuType.DataBind();
            ddlMenuType.Items.Insert(0, new ListItem("----Chọn loại danh mục----", string.Empty));
        }

        protected void grdMenu_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdMenu.PageIndex = e.NewPageIndex;
            BinGrid();
        }

        protected void grdMenu_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int MenuID = Convert.ToInt32(e.CommandArgument);
            M_Menu M_Menu = new M_Menu();
            B_Menu B_Menu = new B_Menu();
            B_Insert_Update_Delete bll = new B_Insert_Update_Delete();
            if (e.CommandName.Equals("Delete", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    bll.delete("Menu", "MenuID", M_Menu, MenuID);
                    Response.Write("<script>alert('Đã được xóa!')</script>");
                }
                catch (System.Exception ex)
                {
                    throw new Exception("Đã có lỗi xảy ra! Vui lòng thử lại lần sau!", ex);
                }
                BinGrid();
            }
            if (e.CommandName.Equals("Edit", StringComparison.OrdinalIgnoreCase))
            {
                panAdd.Visible = true;
                btnAdd.Visible = false;
                btnEdit.Visible = true;
                lblTitle.Text = "Cập nhật";
                Bin_dllMenuParent();
                Bin_ddlMenuType();
                M_Menu = B_Menu.GetByPK(MenuID);
                int MenuParentID = Convert.ToInt32(M_Menu.menuParentID.ToString());
                int MenuTypeID = Convert.ToInt32(M_Menu.menuTypeID.ToString());
                if (MenuParentID == 0)
                {
                }
                else
                {
                    ddlMenuParent.SelectedValue = MenuParentID.ToString();
                }
                if (MenuTypeID == 0)
                {
                }
                else
                {
                    ddlMenuType.SelectedValue = MenuTypeID.ToString();
                }
                if (M_Menu.isLock.ToString() == "True")
                {
                    rbYes.Checked = true;
                }
                else
                {
                    rbNo.Checked = true;
                }
                txtMenuName.Text = M_Menu.menuName.ToString();
                txtTitle.Text = M_Menu.title.ToString();
                txtDescription.Text = M_Menu.description.ToString();
                txtSort.Text = M_Menu.sort.ToString();
                btnEdit.CommandArgument = MenuID.ToString();
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            panAdd.Visible = true;
            btnAdd.Visible = true;
            btnEdit.Visible = false;
            lblTitle.Text = "Thêm mới";
            txtMenuName.Text = "";
            txtTitle.Text = "";
            txtSort.Text = "";
            txtDescription.Text = "";
            rbYes.Checked = true;
            Bin_dllMenuParent();
            Bin_ddlMenuType();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            panAdd.Visible = false;
            btnEdit.Visible = true;
            M_Menu M_Menu = new M_Menu();
            M_Menu.menuName = txtMenuName.Text;
            M_Menu.title = txtTitle.Text;
            M_Menu.sort = Convert.ToInt32(txtSort.Text);
            M_Menu.description = txtDescription.Text;
            int MenuParentID = 0;
            if (ddlMenuParent.SelectedValue.ToString() != "" && ddlMenuParent.SelectedValue.ToString() != null)
            {
                MenuParentID = Convert.ToInt32(ddlMenuParent.SelectedValue.ToString());
            }
            int MenuTypeID = 0;
            if (ddlMenuType.SelectedValue.ToString() != "" && ddlMenuType.SelectedValue.ToString() != null)
            {
                MenuTypeID = Convert.ToInt32(ddlMenuType.SelectedValue.ToString());
            }
            if (rbYes.Checked == true)
            {
                M_Menu.isLock = true;
            }
            else
            {
                M_Menu.isLock = false;
            }
            M_Menu.menuParentID = MenuParentID;
            M_Menu.menuTypeID = MenuTypeID;
            B_Insert_Update_Delete bll = new B_Insert_Update_Delete();
            bll.insert("Menu", "MenuID", M_Menu);
            BinGrid();
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            panAdd.Visible = false;
            btnEdit.Visible = true;
            M_Menu M_Menu = new M_Menu();
            M_Menu.menuID = Convert.ToInt32(btnEdit.CommandArgument);
            int MenuID = Convert.ToInt32(btnEdit.CommandArgument);
            M_Menu.menuName = txtMenuName.Text;
            M_Menu.title = txtTitle.Text;
            M_Menu.sort = Convert.ToInt32(txtSort.Text);
            int MenuParentID = 0;
            if (ddlMenuParent.SelectedValue.ToString() != "" && ddlMenuParent.SelectedValue.ToString() != null)
            {
                MenuParentID = Convert.ToInt32(ddlMenuParent.SelectedValue.ToString());
            }
            int MenuTypeID = 0;
            if (ddlMenuType.SelectedValue.ToString() != "" && ddlMenuType.SelectedValue.ToString() != null)
            {
                MenuTypeID = Convert.ToInt32(ddlMenuType.SelectedValue.ToString());
            }
            if (rbYes.Checked == true)
            {
                M_Menu.isLock = true;
            }
            else
            {
                M_Menu.isLock = false;
            }
            M_Menu.menuParentID = MenuParentID;
            M_Menu.menuTypeID = MenuTypeID;
            M_Menu.description = txtDescription.Text;
            B_Insert_Update_Delete bll = new B_Insert_Update_Delete();
            bll.update("Menu", "MenuID", M_Menu, MenuID);
            BinGrid();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            panAdd.Visible = false;
            btnAdd.Visible = true;
            btnEdit.Visible = true;
            txtMenuName.Text = "";
            txtTitle.Text = "";
            txtSort.Text = "";
            txtDescription.Text = "";
            BinGrid();
        }

        protected void grdMenu_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            BinGrid();
        }

        protected void grdMenu_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdMenu.EditIndex = e.NewEditIndex;
            BinGrid();
        }

        protected void ddlMenuType_SelectedIndexChanged(object sender, EventArgs e)
        {
            int MenuTypeID = 0;
            if (!string.IsNullOrEmpty(ddlMenuType.SelectedValue))
            {
                MenuTypeID = int.Parse(ddlMenuType.SelectedValue);
            }
            List<M_Menu> lstM_Menu = (new B_Menu()).GetBy_MenuTypeID(MenuTypeID);
            ddlMenuParent.DataSource = lstM_Menu;
            ddlMenuParent.DataValueField = "MenuID";
            ddlMenuParent.DataTextField = "MenuName";
            ddlMenuParent.DataBind();
            ddlMenuParent.Items.Insert(0, new ListItem("----Chọn danh mục cha----", string.Empty));
        }
    }
}